package com.topwalk.datacollect.controller;


import com.topwalk.datacollect.enumI.FactoryEnum;
import com.topwalk.datacollect.factoty.ServerFactory;
import com.topwalk.datacollect.manager.FactoryManager;
import com.topwalk.datacollect.server.BaseServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@RestController
@RequestMapping("/collectsvc")
public class TaskController {
    private static final Logger LOG = LoggerFactory.getLogger(IncController.class);

    @Autowired
    private FactoryManager factoryManager;

    @PostMapping(value = {"MtpTask", "DtpTask"})
    public String tasks(String data, HttpServletRequest request) {
        // 获取请求接口
        StringBuffer url = request.getRequestURL();
        String remoteHost = request.getRemoteHost();
        int index = url.lastIndexOf("/");
        String api = url.substring(index + 1);
//        String api = "test";
        LOG.info("接收restful：" + api);

        ServerFactory serverFactory = (ServerFactory) factoryManager.getFactory(FactoryEnum.SERVER.toString());
        BaseServer<String> serverImpl = serverFactory.getServerImpl(api, String.class);

        ArrayList<String> args = new ArrayList<>();
        args.add(data);
        args.add(remoteHost);
        String result = serverImpl.handleBiz(args);

        return result;
    }

}
